package com.mapapp.flutter_mapapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mapapp.flutter_mapapp.MainActivity;
import com.mapapp.flutter_mapapp.R;
import com.mapapp.flutter_mapapp.application.Constants;
import com.mapapp.flutter_mapapp.application.MyApplication;
import com.mapapp.flutter_mapapp.bean.AvatarBean;
import com.mapapp.flutter_mapapp.bean.LogEntity;
import com.mapapp.flutter_mapapp.bean.LogEntityDao;
import com.mapapp.flutter_mapapp.http.ProgressSubscriber;
import com.mapapp.flutter_mapapp.http.Url;
import com.mapapp.flutter_mapapp.persenter.MinePresenter;
import com.mapapp.flutter_mapapp.util.GlideApp;
import com.mapapp.flutter_mapapp.util.MyActivityManager;
import com.mapapp.flutter_mapapp.util.SelectPhotoUtil;
import com.mapapp.flutter_mapapp.util.SharePreferenceUtil;
import com.mapapp.flutter_mapapp.view.MineViewInterface;
import com.mapapp.flutter_mapapp.widget.SimpleLoadDialog;
import com.zhihu.matisse.Matisse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

public class MineActivity extends ImmersiveBaseActivity implements MineViewInterface {
    private static final String TAG = "MineActivity";

    @BindView(R.id.ll_map_bottom)
    LinearLayout llMapBottom;
    @BindView(R.id.ll_search_bottom)
    LinearLayout llSearchBottom;
    @BindView(R.id.ll_mine_bottom)
    LinearLayout llMineBottom;
    @BindView(R.id.iv_map_bottom)
    ImageView ivMapBottom;
    @BindView(R.id.iv_search_bottom)
    ImageView ivSearchBottom;
    @BindView(R.id.iv_mine_bottom)
    ImageView ivMineBottom;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.iv_avatar)
    CircleImageView ivAvatar;

    private static final int REQUEST_CODE = 1991;

    final ArrayList<String> mSelected = new ArrayList<>();

    private MinePresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine);
        ButterKnife.bind(this);
        presenter = new MinePresenter(this, this, lifecycleSubject);
        String save_username = (String) SharePreferenceUtil.get(mContext, Constants.USERNAME, "");
        tvName.setText(save_username);
        ivMineBottom.setImageResource(R.mipmap.icon_barclick_mine);

        presenter.getAvatar();

        LogEntityDao logEntityDao = MyApplication.getInstance().getDaoSession().getLogEntityDao();
        List<LogEntity> logEntities = logEntityDao.loadAll();
        if (logEntities != null) {
            for (LogEntity logEntity : logEntities) {
                if (logEntity != null && logEntity.getType() != null) {
                    switch (logEntity.getType()) {
                        case "0":
                            presenter.submitOperateInfo("0", logEntity.getRegionId(), null, null, logEntity);
                            break;
                        case "1":
                            presenter.submitOperateInfo("1", logEntity.getRegionId(), logEntity.getProcessId(), null, logEntity);
                            break;
                        case "2":
                            presenter.submitOperateInfo("2", logEntity.getRegionId(), logEntity.getProcessId(), logEntity.getFixType(), logEntity);
                            break;
                        case "3":
                            presenter.submitOperateInfo("3", logEntity.getRegionId(), null, null, logEntity);
                            break;
                    }
                    logEntityDao.delete(logEntity);
                }
            }
        }
    }


    @OnClick({R.id.ll_map_bottom, R.id.ll_search_bottom, R.id.ll_my_task, R.id.ll_error_correction,
            R.id.ll_my_data, R.id.ll_upload_data, R.id.btn_mine_change_password, R.id.tv_mine_logout, R.id.iv_avatar})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_avatar:
                SelectPhotoUtil.openPhotoAlbum(this, REQUEST_CODE, 1);
                break;
            case R.id.ll_map_bottom:
                startActivity(new Intent(mContext, MainActivity.class));
                MyActivityManager.getActivityManager().pop(this);
                break;
            case R.id.ll_search_bottom:
                startActivity(new Intent(mContext, SearchActivity.class));
                MyActivityManager.getActivityManager().pop(this);
                break;
            case R.id.ll_my_task:
                startActivity(new Intent(mContext, MyTaskActivity.class));
                break;
            case R.id.ll_error_correction:
                startActivity(new Intent(mContext, MyErrorCorrectionActivity.class));
                break;
            case R.id.ll_my_data:
                startActivity(new Intent(mContext, MyDataActivity.class));
                break;
            case R.id.ll_upload_data:
                startActivity(new Intent(mContext, UploadDataActivity.class));
                break;
            case R.id.btn_mine_change_password:
                Intent intent = new Intent(mContext, EditPasswordActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_mine_logout:
                startActivity(new Intent(mContext, LoginActivity.class));
                SharePreferenceUtil.put(mContext, Constants.USERNAME, "");
                SharePreferenceUtil.put(mContext, Constants.PASSWORD, "");
                MyActivityManager.getActivityManager().popAll();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE) {
                if (Matisse.obtainPathResult(data) != null) {
                    mSelected.clear();
                    mSelected.addAll(Matisse.obtainPathResult(data));
                    if (!mSelected.isEmpty()) {
                        presenter.uploadAvatar(mSelected.get(0));
                    }
                }
            }
        }

    }

    @Override
    public void showResult(boolean isSuccess) {
        if (isSuccess) {
            if (!mSelected.isEmpty()) {
                GlideApp.with(mContext).load(mSelected.get(0)).placeholder(R.mipmap.ic_header).error(R.mipmap.ic_header).into(ivAvatar);
            }
        }
    }

    @Override
    public void showAvatarInfo(AvatarBean avatarBean) {
        if (avatarBean == null) {
            return;
        }

        String url = Url.BASE_URL + "/common/output/image/" + avatarBean.getPhoto_folder()
                + "/" + avatarBean.getAppAvatar() + "/ygf";
        GlideApp.with(mContext).load(url).placeholder(R.mipmap.ic_header).error(R.mipmap.ic_header).into(ivAvatar);
    }

    @Override
    public void uploadLog(boolean isSuccess, LogEntity entity) {
        if (entity != null) {
            MyApplication.getInstance().getDaoSession().getLogEntityDao().delete(entity);
        }
    }
}
